﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Manager;
using Model;
using ExtAspNet;
using Manager.CustomerHelper;
namespace HealthSystem
{
    public partial class Ord_New :BasePage
    {
        HW_OrgManager manager = new HW_OrgManager();
        public override ModuleType Module
        {
            get
            {
                return ModuleType.OrgManager;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();
                choosedistric.OnClientClick = Window1.GetSaveStateReference(districtname.ClientID, districtid.ClientID)
                   + Window1.GetShowReference("dischoose.aspx","选择地区");
            }
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            try
            {
                HW_DistrictManager districtmanager = new HW_DistrictManager();
                string districtcode = districtid.Text;

                var dt= districtmanager.GetManager(districtcode);

                string dis = dt.Rows[0]["XX_UserName"] as string;
                dis = dis.Substring(0, 3);

                HW_OrgUserManager usermanager = new HW_OrgUserManager();
                string temp= usermanager.GetMaxUser("M-"+dis);
                if (string.IsNullOrEmpty(temp))
                    temp = "M-" + dis + "001";

                HW_Org org = new HW_Org();
                org.hw_OrgDetail = new HW_OrgDetail();
                org.OrgCode = "M-" + dis + (Convert.ToInt32(temp.Substring(5))+1+ 1000).ToString().Substring(1);
                org.OrgName = this.name.Text;
                org.DistrictCode = this.districtid.Text;

                org.hw_OrgDetail.OrgCode = org.OrgCode;
                org.hw_OrgDetail.Address = Address.Text;
                org.hw_OrgDetail.Email = Email.Text;
                org.hw_OrgDetail.Fax = Fax.Text;
                org.hw_OrgDetail.LinkMan = LinkMan.Text;
                org.hw_OrgDetail.MobilePhone = MobilePhone.Text;
                org.hw_OrgDetail.OfficePhone = OfficePhone.Text;

               
                HW_District hwdistrict = districtmanager.GetById(org.DistrictCode);
                org.CityId = hwdistrict.ParentDistrictCode;
                HW_XXX_User xxxUser = new HW_XXX_User()
                {
                    XX_UserName = org.OrgCode,
                    XX_Pass=System.Configuration.ConfigurationManager.AppSettings["default"] as string,
                    CNName=LinkMan.Text,
                    ErrorTimes=0,
                    RoleId=2000,
                };
                xxxUser.OrgUser = new HW_OrgUser()
                {
                    OrgCode=org.OrgCode,
                    XX_UserName = org.OrgCode,
                    XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"] as string,
                    CNName = LinkMan.Text,
                    ErrorTimes = 0,
                    RoleId = 2000,
                };
                manager.Add(org,xxxUser,xxxUser.OrgUser);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                Alert.Show("新增成功！");
            }
            catch
            {
                Alert.Show("添加机构出错，请检查");
            }
        }
    }
}